package com.zcf.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zcf.common.json.Body;
import com.zcf.mapper.ProductMapper;
import com.zcf.pojo.Product;
import com.zcf.service.ProductService;
import com.zcf.utils.Hutool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    public Body setProduct(String diningId, String productName, String image, Double price, String remark,
                           String productNums, String classId, String isroom, String showcreate, String showend) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(date);
        Product product = new Product();
        product.setId(Hutool.getId());
        product.setDiningId(diningId);
        product.setProductName(productName);
        product.setImage(image);
        product.setPrice(price);
        product.setRemark(remark);
        product.setTime(time);
        product.setProductNums(Integer.parseInt(productNums));
        product.setClassId(classId);
        product.setIsroom(Integer.parseInt(isroom));
        product.setShowcreate(showcreate);
        product.setShowend(showend);
        Integer insert = productMapper.insert(product);
        if (insert > 0) {
            return Body.newInstance(product);
        }
        return Body.newInstance(501, "上传商品失败");
    }

    public Body checkProductList(String diningId, String type, String classId) {
        EntityWrapper<Product> ew = new EntityWrapper<Product>();
        ew.eq("dining_id", diningId);
        if (type != null && type != "") {
            ew.eq("type", Integer.parseInt(type));
        }
        if (classId != null && classId != "") {
            ew.eq("class_id", classId);
        }
        List<Product> list = productMapper.selectList(ew);
        if (!list.isEmpty()) {
            return Body.newInstance(list);
        }
        return Body.newInstance(501, "商家未有商品");

    }

    public Body checkProductListAll(String diningId) {
        EntityWrapper<Product> ew = new EntityWrapper<Product>();
        ew.eq("dining_id", diningId);
        List<Product> list = productMapper.selectList(ew);
        if (!list.isEmpty()) {
            return Body.newInstance(list);
        }
        return Body.newInstance(501, "商家未有商品");

    }

}
